<?php 
	$user_id=$gender=$address=$remark=$phone=$fullName = $userLog = $pass = $confirm = $email = $gro_id = $active = $filename="";
	$control ="insert";
	$btn = "Save";
	//check when insert or update
	if(isset($_REQUEST['control'])){
		$fieldimage = "";
		$gro_id=$_POST['cbogro_id'];
		$cat_id=$_POST['cbocat_id'];
		$gender=$_POST['txtgender'];
		$fullName=escapeValue($_POST['txtfullName']);
		$userLog=escapeValue($_POST['txtuserLog']);
		
		$pass=$_POST['txtpass'];
		$confirm=$_POST['txtconfirm'];
		if($pass!="" AND $confirm!=""){
			$pass=md5($pass);
			$confirm=md5($confirm);
		}
		
		$active=$_POST['cboactive'];
		$email=escapeValue($_POST['txtemail']);
		$phone=escapeValue($_POST['txtphone']);
		$address=escapeValue($_POST['txtaddress']);
		$filename = escapeValue($_FILES["uploadfile"]["name"]);
		$remark=escapeValue($_POST['txtremark']);
		$user_name=getUserName();
		$oldfilename=$_POST['oldfilename'];
		
		if($pass!=$confirm){
			errorMessage("Password is not match...!");
		}else{
			include("user_verify.php");
		}
	}
	// Select user info to fill in control
	if(isset($_GET['user_id'])){
		$querySelectProduct = "SELECT * FROM tbl_user WHERE user_id= $_GET[user_id]";
		$result = mysql_query($querySelectProduct);
		$row = mysql_fetch_array($result);
			$user_id=$_GET['user_id'];
			$gro_id = $row['gro_id'];
			$cat_id = $row['cat_id'];
			$gender = $row['gender'];
			$fullName=$row['full_name'];
			$userLog = $row['user_login'];
			//$confirm = $row['password'];
			$active = $row['active'];
			$email = $row['email'];
			$phone=$row['phone'];
			$address=$row['address'];
			$filename = $row['photo'];
			$remark=$row['remark'];
			
			$control = "update&user_id=".$_GET['user_id'];		
			$btn = "Update";	
	}
?>


 <div class="box box-primary">

 
<div class="box-header">
  <?php if($btn=="Save"){ ?>
 	 	<div class="icon_new"></div>
        <h3 class="box-title">Add new user</h3>
	<?php }elseif($btn=="Update"){?>
   		<div class="icon_edit"></div>
 	 	 <h3 class="box-title">Edit user</h3>
	<?php }?>
</div>
<div class="sms">
  <?php 
  if(isset($_REQUEST['msgStatus'])){
      echo $_REQUEST['msgStatus'];
  }else{echo @$errors;}
  ?>
</div>
<div style="clear:both"	></div>

<div  class="content-header">

<form class="form" role="form" id="form1" name="form1" action="index.php?page=users/user_list&action=users/user_new&control=<?php echo $control;?>" method="post"  enctype="multipart/form-data" >
 <div class="col-md-6">
<div class="box-body">    
    	<div class="form-group">
            	<label>User Type:</label>
				<select class="form-control" name="cbogro_id" id="cbogro_id" >
				   <?php
                   
                   $sql="select * from tbl_group ";
                   $sql_row=mysql_query($sql);
                   
                   while($sql_res=mysql_fetch_assoc($sql_row))
                   {
                       if(@$gro_id == $sql_res['gro_id'])
                                $selected = " selected='selected'";
                            else	
                                $selected = "";
                   ?>
                   <option value="<?php echo $sql_res["gro_id"]; ?>"<?php echo $selected;?>><?php echo $sql_res["gro_name"];?></option>
                   <?php
                   }
                   ?>
               </select>
         </div>
		
        <div class="form-group">
            	<label>Category:</label>
                <select class="form-control" name="cbocat_id" id="cbocat_id" >
				   <?php 
                   $sql="select * from tbl_category ";
                   $sql_row=mysql_query($sql);
                   while($sql_res=mysql_fetch_assoc($sql_row))
                   {
                       if(@$cat_id == $sql_res['cat_id'])
                                $selected = " selected='selected'";
                            else	
                                $selected = "";
                   ?>
                   <option value="<?php echo $sql_res["cat_id"]; ?>"<?php echo $selected;?>><?php echo $sql_res["cat_name"];?></option>
                   <?php
                   }
                   ?>
               </select>
		</div>
		<div class="form-group">
            	<label><span class="red">*</span> Full Name:</label>
                <input type="text" class="form-control" minlength="1" name="txtfullName" value="<?php echo $fullName ?>" />
		</div>
        <div class="form-group">
            	<label><span class="red">*</span> User Log:</label> 
        <input type="text" class="form-control" minlength="1" name="txtuserLog" value="<?php echo $userLog ?>" />
		</div>
        <div class="form-group">
            	<label><span class="red">*</span> Gender:</label> 
              	<select name="txtgender" class="form-control">
				<?php
                if($gender=="male")
                {
                    echo "<option value=".$gender." selected='selected'>Male</option>";
                    echo "<option value='female'>Female</option>";
                }
                elseif($gender=="female")
                {
                    echo "<option value='male'>Male</option>";
                    echo "<option value=".$gender." selected='selected'>Female</option>";
                }else{
                    echo "<option value='male' selected='selected'>Male</option>";
                    echo "<option value='female'>Female</option>";
                }
              ?>
                </select>
         </div>
        <?php
			if($user_id<>""){
		?>
        <div class="form-group">
            	<label><span class="red">*</span>Password:</label>
                <td><input  type="password" class="form-control"  name="txtpass" value="" />
		</div>
        <div class="form-group">
            	<label><span class="red">*</span>Confirm:</label>
			<input type="password" class="form-control" name="txtconfirm" value="" />
		</div>
        <?php
			}else{
		?>
       <div class="form-group">
            	<label><span class="red">*</span>Password:</label>
				<input type="password"class="form-control" minlength="1" name="txtpass" value="<?php echo $pass ?>" />
		</div>
       
	   <div class="form-group">
            	<label><span class="red">*</span>Confirm:</label>
            </td>
            <td><input type="password" class="input req-min" minlength="1" name="txtconfirm" value="<?php echo $confirm ?>" />
		</div>
		 <?php
			}
        ?>
       <div class="form-group">
            	<label>Active:</label>
           	    <select name="cboactive" class="form-control">
				  <?php
				  if($active=="1")
				  {
					  echo "<option value=".$active." selected='selected'>Active</option>";
					  echo "<option value='0'>Deactive</option>";
				  }
				  elseif($active=="0")
				  {
					  echo "<option value='1'>Active</option>";
					  echo "<option value=".$active." selected='selected'>Deactive</option>";
				  }else{
					  echo "<option value='1' selected='selected'>Active</option>";
					  echo "<option value='0'>Deactive</option>";
				  }
                   ?>
                </select>
          </div>
</div>
</div>
 <div class="col-md-6">
<div class="box-body">

       <div class="form-group">
	       	<label><span class="red">*</span>Email:</label>
			<input type="text" class="form-control" placeholder="Email" maxlength="50" name="txtemail" value="<?php echo $email ?>" />
		</div>
		
       <div class="form-group">
            	<label><span class="red">*</span>Phone Number:</label>
            </td>
            <td><input type="text" class="form-control" placeholder="Phone Number" minlength="1" name="txtphone" value="<?php echo $phone ?>" />
		</div>
        
		<div class="form-group">
            	<label><span class="red">*</span>Address:</label>
                <td><input type="text" class="form-control" placeholder="Enter ..." id="txtPending" class="input req-min" minlength="1" name="txtaddress" value="<?php echo $address ?>" />
		</div>
        
		<div class="form-group">
            	<label>Remark:</label>
                <textarea class="form-control" rows="3" placeholder="Enter ..." name="txtremark"><?php echo $remark ?></textarea>
        </div>
		<div class="form-group">
        	<td>Upload Your Photo: </td>
            <td>
            	<input class="btn btn-success" type="file" name="uploadfile"  />
                <input type="hidden" name="oldfilename" value="<?php echo $filename;?>" />
                <img src="users/<?php echo $filename;?>" width="80" height="100" />
          </div>
		<div class="form-group">
		
                <input class="btn btn-success" type="submit"  name="Submit" id="Submit" value="<?php echo $btn ?>" />&nbsp; 
                <input type="button" class="btn btn-warning" name="btnCancel" value="Cancel" onclick="redirect('index.php?page=users/user_list&action=users/user_list')" />
        </div>
</div>
</div>
</div>
</form>
</div>
</div>
